package org.budo.activiti.controller;

import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.budo.activiti.service.api.BudoActivitiService;
import org.budo.support.freemarker.FreemarkerUtil;
import org.budo.support.lang.util.IoUtil;
import org.budo.support.mvcs.Mvcs;
import org.budo.support.servlet.util.ResponseUtil;
import org.budo.support.spring.io.util.ResourceUtil;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import lombok.extern.slf4j.Slf4j;

/**
 * 流程控制器
 */
@Slf4j
public class BudoActivitiController extends AbstractActivitiController{
    @Resource
    private BudoActivitiService budoActivitiService;

    /**
     * 静态资源 1
     */
    @ResponseBody
    @RequestMapping("static/**")
    public void _static(HttpServletRequest request, HttpServletResponse response) {
        this.handleStaticRequest(request, response);
    }

    /**
     * 静态资源 2
     */
    @ResponseBody
    @RequestMapping("editor-app/**")
    public void editorApp(HttpServletRequest request, HttpServletResponse response) {
        this.handleEditorAppRequest(request, response);
    }

    /**
     * 获取流程json文件
     */
    @ResponseBody
    @RequestMapping(value = "service/editor/stencilset", produces = "application/json;charset=utf-8")
    public void getStencilset(HttpServletResponse response) {
        InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
        IoUtil.write(stencilsetStream, ResponseUtil.getOutputStream(response));
    }

    /**
     * 获取流程json信息
     */
    @ResponseBody
    @RequestMapping(value = "service/model/{modelId}/json", method = RequestMethod.GET, produces = "application/json")
    public void findModelEditorJson(@PathVariable String modelId, HttpServletResponse response) {
        String editorJson = budoActivitiService.findModelEditorJson(modelId);
        IoUtil.write(editorJson, ResponseUtil.getOutputStream(response));
    }

    /**
     * 跳转流程展示页面
     */
    @ResponseBody
    @RequestMapping("activiti_model_editor")
    public void editor_2(HttpServletResponse response) {
        InputStream inputStream = ResourceUtil.classPathResourceInputStream("templates/activiti_model_editor.htm");
        FreemarkerUtil.freemarkerRender(inputStream, null, ResponseUtil.getOutputStream(response));
    }

    /**
     * 创建模型
     * 
     * @param response
     * @param name     模型名称
     * @param key      模型key
     */
    @RequestMapping("activiti_model_create")
    public void createModel(HttpServletResponse response, String name, String key) {
        log.info("#107 创建模型入参name：{}, key:{}", name, key);

        String modelId = budoActivitiService.createModel(name, key);

        Mvcs.redirect("activiti_model_editor?modelId=" + modelId);
        log.info("#126 创建模型结束，返回模型ID：{}", modelId);
    }

    /**
     * 保存流程
     * 
     * @param modelId 模型ID
     * @param name 流程模型名称
     * @param description
     * @param json_xml 流程文件
     * @param svg_xml 图片
     */
    @ResponseBody
    @RequestMapping(value = "service/model/{modelId}/save")
    @ResponseStatus(value = HttpStatus.OK)
    public void saveModel(@PathVariable String modelId, String name, String description, String json_xml, String svg_xml) {
        budoActivitiService.saveModel(modelId, name, description, json_xml, svg_xml);

        budoActivitiService.deployModel(modelId);
    }
}
